﻿<%@ WebHandler Language="C#" Class="CommonHandler" %>

using System;
using System.Web;
using System.Collections;
using System.Data;
using StarTech.DBUtility;
using System.Text;
using System.IO;



public class CommonHandler : IHttpHandler
{
    AdoHelper ado = AdoHelper.CreateHelper("DB_Instance");
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        string flag = Common.NullToEmpty(context.Request["flag"]);

        switch (flag)
        {
            case "uploadfile":
                if (context.Request.Files.Count == 0)
                {
                    context.Response.Write("0");  //代表文件为空
                }
                else
                {
                    string table = Common.NullToEmpty(context.Request["table"]);
                    string key = Common.NullToEmpty(context.Request["key"]);
                    string value = Common.NullToEmpty(context.Request["value"]);
                    string prop = Common.NullToEmpty(context.Request["prop"]);
                    context.Response.Write(Common.SaveFile(table, key, value, prop, context.Request.Files[0]));
                }
                break;
            case "ad":
                context.Response.Write(GetAd());
                break;
            case "aboutus":
                context.Response.Write(GetAboutUs());
                break;
        }

    }

    private string GetAd() 
    {
        string strSql = "select AdId,title,Link,Image,sort from T_Ad where starttime<='"+DateTime.Now.ToString()+"' and endtime>'"+DateTime.Now.ToString()+"' order by sort";
        DataTable dt = ado.ExecuteSqlDataset(strSql).Tables[0];
        return JSONHelper.GetJSON(dt);
    }

    private string GetAboutUs()
    {
        return System.IO.File.ReadAllText(System.AppDomain.CurrentDomain.BaseDirectory+"..\\Web\\aboutUs.txt");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}